/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MyDate;

/**
 * @author thomas.s
 */

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import sUtils.*;


public class MyDate extends MIDlet implements CommandListener, EventListener {
    MyDateView canvas = null;
    public PIMReader pim = new PIMReader();
    TextBox tb = null;
    TextInput ti = null;
    TimeChoice tc = null;
    String dateText = "";
    MyDateEntry dateEntry = null;
    VolumeKeys vkeys = null;

    public Command exit, edit, ok, cancel, evRemove, /*evEdit,*/ menu, spare;

    public MyDate() 
    {
        Lang.init();
        menu = new Command (Lang.word("menu","Menü"), Command.ITEM, 0);
        spare = new Command ("", Command.ITEM, 0);
        exit = new Command ("X", Command.ITEM, 1);
        edit = new Command (Lang.word("New","Neu"), Command.ITEM, 1);
        ok      = new Command ("Ok", Command.ITEM, 1);
        cancel  = new Command (Lang.word("Cancel","Zurück"), Command.ITEM, 0);
        evRemove = new Command(Lang.word("Remove","Löschen"), Command.ITEM, 1);
        //evEdit   = new Command(Lang.word("Edit","Bearbeiten"), Command.ITEM, 1);
    }
    public void up() { canvas.scroll(-14); }
    public void down() { canvas.scroll(14); }

    public void startApp()
    {
       canvas = new MyDateView(this);
       canvas.addCommand (spare);
       canvas.addCommand (menu);
       canvas.setCommandListener(this);
       vkeys = new VolumeKeys(this);
       Display.getDisplay(this).setCurrent( canvas );

    }
    public void pauseApp() {}
    public void destroyApp(boolean unconditional) {}
    public void close()
    {
        vkeys.stop();
        if (!canvas.slider)
        {
            canvas.close();
            canvas = null;
            tb = null;
            tc = null;
            destroyApp(true);
            notifyDestroyed();
        }
    }
    public void newEvent()
    {
        dateText="";
        dateEntry=null;
        ti = new TextInput(this,dateText);
        ti.addCommand(cancel);
        ti.addCommand(ok);
        ti.setCommandListener(this);
        Display.getDisplay(this).setCurrent(ti);
    }
    public void showCurrent()
    {
        MyDateDay d=canvas.getCurrent();
        //parent.edit();
        d.show(this);
        d.removeCommand(cancel);
        //d.addCommand(evEdit);
        d.addCommand(evRemove);
        d.addCommand(cancel);
        d.setCommandListener(this);
    }
    public void commandAction(Command c, Displayable s) {
        if (c == menu) canvas.showMenu(); else
        if (c == exit) close(); else
        if (c == edit) newEvent(); else
        if (c == evRemove /*|| c== evEdit*/)
        {
            //canvas.writeInfoHTML((MyDateDay)s);
            Display.getDisplay(this).setCurrent(canvas);

            MyDateDay day = (MyDateDay)s;
            dateEntry = day.getCurrent();
            if (dateEntry!=null)
            {
                if (c==evRemove) 
                {
                    if (pim.removeEvent(dateEntry.uid))
                        canvas.removeEntry(dateEntry);
                }
                else
                {
                    dateText=dateEntry.getText();
                    ti = new TextInput(this,dateEntry.getText());
                    ti.addCommand(cancel);
                    ti.addCommand(ok);
                    ti.setCommandListener(this);
                    Display.getDisplay(this).setCurrent(ti);
                }
            }
        } else
        if (c == ok || c==cancel)
        {
            
            if (c==ok) {
                if (tb!=null || ti!=null)
                {
                    dateText = tb!=null ? tb.getString() : ti.getString();
                    tb = null;
                    ti = null;
                    if (dateText!=null && dateText.length()>0)
                    {
                        tc = new TimeChoice();
                        if (dateEntry!=null)
                        {
                            int min = dateEntry.getMinutes();
                            tc.setTimes( min/60, min%60, dateEntry.getLen(), 0 );
                        }
                        tc.addCommand(cancel);
                        tc.addCommand(ok);
                        tc.setCommandListener(this);
                        Display.getDisplay(this).setCurrent(tc);
                        return;
                    }
                }
                else
                if (tc!=null)
                {
                    try {
                        MyDateEntry neu = canvas.setText(dateEntry,dateText,tc.hh,tc.mm,tc.len,tc.alarm);
                        if (dateEntry!=null) canvas.updateEntry(dateEntry); else canvas.addEntry(neu);

                        Display.getDisplay(this).vibrate(400);
                    } catch( Exception e ) {}
                }
            }
            //MyDateDay d=canvas.getCurrent();
            //d.refresh();

            Display.getDisplay(this).setCurrent(canvas);
            tb = null;
            tc = null;
            ti = null;
            dateEntry=null;
        }
    }

    public void onEvent( String id, String data )
    {
        if (id.equalsIgnoreCase("volume"))
        {
            if (data.equalsIgnoreCase("-1")) up(); else down();
        }
    }

    public void onMenu( String id )
    {
        if (id!=null)
        {
            if (id.equalsIgnoreCase("quit")) close(); else
            if (id.equalsIgnoreCase("new")) newEvent(); else;
            if (id.equalsIgnoreCase("show")) showCurrent(); else;/*
            if (id.equalsIgnoreCase("quit")) close(); else
            if (id.equalsIgnoreCase("quit")) close(); else
            if (id.equalsIgnoreCase("quit")) close(); else
            if (id.equalsIgnoreCase("quit")) close(); else*/

        }
    }

   
}
